<template>
    <div class="shopcar-container">
        <div class="mui-card" v-for="item in goodslist" :key="item.id">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="goods-box">
                        <mt-switch v-model="$store.getters.getStatus[item.id]" 
                        @change="getSelectedStatus(item.id, $store.getters.getStatus[item.id])"></mt-switch>
                        <img :src="item.thumb_path" alt="">
                        <div class="buy-box">
                            <h3 class="title">{{ item.title }}</h3>
                            <div class="order">
                                <span class="price">￥{{ item.sell_price }}</span>
                                <shop-box :initnum="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></shop-box>
                                <span class="delete">删除</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="calculate">
                        <div>
                            <p>总计（不含运费）</p>
                            <p>已勾选商品<span>{{ $store.getters.getAllPrice["count"] }}</span>件，总价：
                            <span>￥{{ $store.getters.getAllPrice["price"] }}</span></p>
                        </div>
                        <mt-button type="danger">去结算</mt-button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import shopcar_numbox from "../subcomponent/shopcar-numbox.vue"
export default {
    data(){
        return {
            goodslist: []
        }
    },
    created(){
        this.getGoodslist()
    },
    methods: {
        getGoodslist(){   // 获取购物车中货物列表
            var list = []
            this.$store.state.car.forEach(item => list.push(item.id));
            this.$http.get('api/goods/getshopcarlist/' + list.join(",")).then(result => {
                if(result.body.status === 0){
                    this.goodslist = result.body.message
                }
            })
        },
        getSelectedStatus(id, status){   // 更新本地浏览器缓存数据
            this.$store.commit("updateSelectedStatus", {
                id: id,
                status: status
            })
        }
    },
    components: {
        "shop-box": shopcar_numbox
    }
}
</script>

<style lang="scss" scoped>
.shopcar-container{
    .goods-box{
        display: flex;
        flex-wrap: nowrap;
        img{
            padding: 5px;
            width: 80px;
        }
        .mui-numbox{
            width: 110px;
            height: 30px;
        }
        .buy-box{
            display: flex;
            flex-direction: column;
            width: 60%;
            .title{
                font-size: 14px;
            }
            .order{
                margin-top: 8px;
                .price{
                    color: red;
                }
                .delete{
                    color: #226aff;
                }
            }
        }
    }
    .calculate{
        display: flex;
        justify-content: space-between;
        align-items: center;
        span{
            color: red;
            font-weight: bold;
            font-size: 15px
        }
    }
}
</style>